﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OAProject.Domain.Entities;
using OAProject.Domain.Interfaces;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.AspNetCore.Authorization;

namespace OAProject.API.Controllers
{
    /// <summary>
    /// 字典API控制器
    /// </summary>
    [Route("api/[controller]/[Action]")]
    [ApiController]
    [Authorize]     //全局授权过滤器
    public class DicAPIController : ControllerBase
    {
        public IDictValueRepository _DictValue;            //字典值
        public IDictKeyRepository _dictKeyRepository;      //字典键
        public IDictsValueRepository _dictsValueRepository;//两边联查显示
        public DicAPIController(IDictKeyRepository dictKeyRepository, IDictValueRepository dictValueRepository, IDictsValueRepository dictsValueRepository)
        {
            this._DictValue = dictValueRepository;
            this._dictKeyRepository = dictKeyRepository;
            this._dictsValueRepository = dictsValueRepository;
        }
        /// <summary>
        /// 字典添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add([FromForm] DictValue m)
        {
            var s = _DictValue.Insert(m);
            return Ok(s);
        }
        /// <summary>
        /// 字典显示
        /// </summary>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Show(int parentID = 0, string keyName = "", int page = 1, int limit = 10)
        {
            List<DictValues> users = _dictsValueRepository.ShowDic();
            //查询所有带条件的
            if (keyName!=null)
            {
                var all = users.Where(x => x.KeyName.Contains(keyName)).ToList();
                //查询所有带条件并且查询子类目录下的数据
                var submenu = users.Where(x => x.KeyName.Contains(keyName) && x.KeyID.Equals(parentID)).ToList();
                if (parentID == 0 || parentID == 1)
                {
                    var list = all.Skip((page - 1) * limit).Take(limit).ToList();
                    return Ok(new
                    {
                        data = list,
                        code = 0,
                        msg = "",
                        count = users.Count
                    });
                }
                else
                {
                    var list = users.Skip((page - 1) * limit).Take(limit).ToList();
                    return Ok(new
                    {
                        data = list,
                        code = 0,
                        msg = "",
                        count = users.Count
                    });
                }
            }
            else
            {
                var list = users.Skip((page - 1) * limit).Take(limit).ToList();
                return Ok(new
                {
                    data = list,
                    code = 0,
                    msg = "",
                    count = users.Count
                });
            }
            
            

            
        }
        /// <summary>
        /// 字典删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Del(int id)
        {
            var s = _DictValue.Delete(id);
            return Ok(s);
        }
        /// <summary>
        /// 字典反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Fan(int id)
        {
            var s = _DictValue.GetById(id);
            return Ok(new
            {
                data = s,
                code = 0
            });
        }
        /// <summary>
        /// 字典修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Uep([FromForm] DictValue m)
        {
            var s = _DictValue.Update(m);
            return Ok(s);
        }

        #region 字典树(拼接字符串)
        /// <summary>
        /// 父类(上一级)
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Shu(int parentID = 0)
        {
            List<DictKey> menus = _dictKeyRepository.Show();
            List<DictKey> menus1 = menus.Where(x => x.ParentID.Equals(parentID)).ToList();
            Show(parentID);

            List<Dictionary<string, object>> data = new List<Dictionary<string, object>>();
            foreach (var item in menus1)
            {
                Dictionary<string, object> json = new Dictionary<string, object>();
                json.Add("id", item.KeyID);
                json.Add("title", item.KeyName);
                json.Add("spread", true);
                Shu_nest(menus, json, item.KeyID);
                data.Add(json);
            }
            return Ok(data);
        }

        /// <summary>
        /// 子类
        /// </summary>
        /// <param name="menus"></param>
        /// <param name="json"></param>
        /// <param name="menuID"></param>
        private void Shu_nest(List<DictKey> menus, Dictionary<string, object> json, int dicId)
        {
            List<DictKey> menus1 = menus.Where(x => x.ParentID.Equals(dicId)).ToList();
            List<Dictionary<string, object>> data = new List<Dictionary<string, object>>();
            if (menus1.Count == 0)
            {
                json.Add("children", null);
                return;
            }
            foreach (var item in menus1)
            {
                Dictionary<string, object> json1 = new Dictionary<string, object>();
                json1.Add("id", item.KeyID);
                json1.Add("title", item.KeyName);
                json1.Add("spread", true);
                Shu_nest(menus, json1, item.KeyID);
                data.Add(json1);
            }
            json.Add("children", data);
        }
        #endregion
    }
}
